<?php

namespace common\controller;

use common\exception\ExceptionEmun;
use common\exception\TokenException;
use common\token\Token;
use think\App;
use think\facade\Request;

class TokenController extends BaseController
{
    /**
     * @var array 设置Ture控制器方法全局跳过权限验证,可以是数组或者字符串
     */
    protected $no_token = [];

    public function __construct(App $app = null)
    {
        parent::__construct($app);

        if (!empty($this->no_token) && check_mixed_exist($this->no_token, $this->request->action())) {
            return true;
        }
        $header = Request::instance()->header();
        //token名多样化
        $token_name = sprintf("%s-%s", Request::module(), "token");
        if (!empty($header[$token_name])) {
            Token::instance()->verifyToken($header[$token_name]);
        } elseif (!empty($header['authkey'])) {
            Token::instance()->verifyToken($header['authkey']);
        } else {
            ExceptionEmun::throwException(TokenException::ILLEGAL_TOKEN);
        }
        $member = Token::instance()->getUserInfo();
        $this->logHistory($member);
        $this->app = $app;
    }

}
